<template>
  <section class="msite">
    <Header :title="address.name || 'Positioning'">
      <span class="header_search" slot="search">
        <i class="iconfont icon-sousuo"></i>
      </span>
      <span class="header_login" slot="loginRegister">
        <span class="header_login_text">登录|注册</span>
      </span>
    </Header>
    <!--首页导航-->
    <nav class="msite_nav">
      <div class="swiper-container"  v-if="categories.length>0">
        <div class="swiper-wrapper">
          <div class="swiper-slide" v-for="(swiperSlide,index) in swiperSlides" :key="index">
            <a href="javascript:" class="link_to_food" v-for="c in categories" :key="c.id">
              <div class="food_container">
                <img :src="`https://fuss10.elemecdn.com${c.image_url}`">
              </div>
              <span>{{c.title}}</span>
            </a>
          </div>
        </div>
        <!-- Add Pagination -->
        <div class="swiper-pagination"></div>
      </div>
      <img src="../images/msite_back.svg" alt="loading" v-else>
    </nav>
    <!--首页附近商家-->
    <Shoplist/>
  </section>
</template>

<script>
  import Swiper from 'swiper'
  import 'swiper/dist/css/swiper.min.css'
  import Shoplist from 'components/Shoplist/shoplist'
  import {mapState} from 'vuex'
  export default {
    name: 'Msite',
    components:{
      Shoplist
    },
    computed:{
      ...mapState({
        address: state => state.msite.address,
        categories: state => state.msite.categories
      }),
      swiperSlides(){
        const slidesArr = []
        let categoriesArr = []
        
        this.categories.forEach((category)=>{
          if(categoriesArr.length ===0){
            slidesArr.push(categoriesArr)
          }
          
          categoriesArr.push(category)
          
          if(categoriesArr.length===8){
            categoriesArr = []
          }
        })
        // console.log(slidesArr)
        return slidesArr
      }
    },
    mounted () {
      this.$store.dispatch('getCategories',()=>{
        this.$nextTick(()=>{
          new Swiper ('.swiper-container', {
            direction: 'horizontal',
            loop: true,
            pagination: {
              el: '.swiper-pagination',
            }
          })
        })
      })
    },
    /*watch:{
        categories(){
        this.$nextTick(()=>{
          new Swiper ('.swiper-container', {
            direction: 'horizontal',
            loop: true,
            pagination: {
              el: '.swiper-pagination',
            }
          })
        })
      }
    }*/
  }
</script>

<style lang="stylus" rel="stylesheet/stylus" scoped>
  @import "../../common/stylus/mixins.styl"
  .msite  //首页
    width 100%
    .msite_nav
      bottom-border-1px(#e4e4e4)
      margin-top 45px
      height 200px
      background #fff
      .swiper-container
        width 100%
        height 100%
        .swiper-wrapper
          width 100%
          height 100%
          .swiper-slide
            display flex
            justify-content center
            align-items flex-start
            flex-wrap wrap
            .link_to_food
              width 25%
              .food_container
                display block
                width 100%
                text-align center
                padding-bottom 10px
                font-size 0
                img
                  display inline-block
                  width 50px
                  height 50px
              span
                display block
                width 100%
                text-align center
                font-size 13px
                color #666
        .swiper-pagination
          >span.swiper-pagination-bullet-active
            background #02a774
</style>
